package leetCode;

public class ImageSmoother {
    public int[][] imageSmoother(int[][] M) {
    	int n=M.length;
    	if(n==0){
    		return new int[0][0];
    	}
    	int m=M[0].length;
    	if(m==0){
    		return new int[n][0];
    	}
        int[][] res=new int[n][m];
        int[][] neibors={{0,0},{0,1},{0,-1},{-1,-1},{-1,0},{1,-1},{-1,1},{1,1},{1,0}};
        for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				int sum=0;
				int count=0;
				for (int[] js : neibors) {
					int newI=i+js[0];
					int newJ=j+js[1];
					if(newI<n&&newI>=0&&newJ<m&&newJ>=0){
						sum+=M[newI][newJ];
						count++;
					}
				}
				res[i][j]=sum/count;
			}
		}
        return res;
    }
}
